package org.an.proj.contacts.net.service.retrofit.upload;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.lang.ref.WeakReference;

/**
 * ProgressListener 实现类，可直接操作UI，ProgressListener接口回调时在线程中
 */
public abstract class UIProgressListener implements ProgressListener {
  private static final int REQUEST_UPDATE = 0x01;

  //处理UI层的Handler子类
  private static class UIHandler extends Handler {
    //弱引用
    private final WeakReference<UIProgressListener> mUIProgressRequestListenerWeakReference;

    public UIHandler(Looper looper, UIProgressListener uiProgressListener) {
      super(looper);
      mUIProgressRequestListenerWeakReference =
        new WeakReference<UIProgressListener>(uiProgressListener);
    }

    @Override public void handleMessage(Message msg) {
      //switch (msg.what) {
      //  case REQUEST_UPDATE:
      UIProgressListener uiProgressListener = mUIProgressRequestListenerWeakReference.get();
      if (uiProgressListener != null) {
        //获得进度实体类
        ProgressInfo progressInfo = (ProgressInfo) msg.obj;
        //回调抽象方法
        uiProgressListener.onUIRequestProgress(progressInfo.getCurrentBytes(),
          progressInfo.getContentLength(), progressInfo.isDone());
      }
      //    break;
      //  default:
      //    super.handleMessage(msg);
      //    break;
      //}
    }
  }

  //主线程Handler
  private final Handler mHandler = new UIHandler(Looper.getMainLooper(), this);

  @Override public void onProgress(long bytesRead, long contentLength, boolean done) {
    //通过Handler发送进度消息
    Message message = Message.obtain();
    message.obj = new ProgressInfo(bytesRead, contentLength, done);
    //message.what = REQUEST_UPDATE;
    mHandler.sendMessage(message);
  }

  /**
   * UI层回调抽象方法
   *
   * @param bytesWrite 当前写入的字节长度
   * @param contentLength 总字节长度
   * @param done 是否写入完成
   */
  public abstract void onUIRequestProgress(long bytesWrite, long contentLength, boolean done);
}